package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCategory;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSAdmin;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskStandardCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 9:21
 */
@RestController
@RequestMapping("/admin/standardCategory")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "计费标准分类")
public class AdminAiRiskStandardCategoryController {

    @Autowired
    private AIRiskStandardCategoryService standardCategoryService;


    @RequiresPermissions("admin:standardCategory:create")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准分类"}, button = "添加")
    @PostMapping("/create")
    @ApiOperation("新增")
    @Transactional
    public Object create(@RequestBody @Validated AIRiskStandardCategory category) {

        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        category.setCreateBy(admin.getUsername());
        category.setUpdateBy(admin.getUsername());
        boolean isSuccess;
        if (Objects.nonNull(category.getId())) {
            isSuccess = standardCategoryService.updateById(category);
        } else {
            isSuccess = standardCategoryService.save(category);
        }

        if (!isSuccess) {
            return ResponseUtil.fail("添加失败");
        }
        return ResponseUtil.ok();
    }


    @RequiresPermissions("admin:standardCategory:delete")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准分类"}, button = "删除")
    @GetMapping("/delete")
    @ApiOperation("删除")
    @Transactional
    public Object delete(@RequestParam Integer id) {
        Boolean bool = standardCategoryService.removeById(id);
        if (bool) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail("删除数据失败");
    }

    @GetMapping("/listCategory")
    @ApiOperation("计算标准分类")
    public Object listCategory(@RequestParam @ApiParam("计费标准ID") Integer chargeId,
                               @RequestParam(defaultValue = "0") @ApiParam("pId") Integer pid
    ) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        List<AIRiskStandardCategory> list = standardCategoryService.getCategory(chargeId, pid);
        list.forEach(item -> {
            Map<String, Object> treeItem = new HashMap<>();
            treeItem.put("id", item.getId());
            treeItem.put("name", item.getName());
            treeItem.put("chargeId", item.getChargeId());
            treeItem.put("pid", item.getPid());
            treeItem.put("sortIndex", item.getSortIndex());
            treeItem.put("isLeaf", item.getIsLeaf());
            treeList.add(treeItem);
        });
        return ResponseUtil.ok(treeList);
    }
}
